package com.ljn.facade.imp;

import com.ljn.core.dto.Enterprise;
import com.ljn.core.dto.Student;
import com.ljn.core.dto.Teacher;
import com.ljn.core.dto.common.Response;
import com.ljn.core.dto.common.User;
import com.ljn.core.dto.common.command.usercommand.IdCommand;
import com.ljn.core.dto.common.command.usercommand.LoginCommand;
import com.ljn.core.dto.common.command.usercommand.Registercommand;
import com.ljn.core.dto.common.command.usercommand.ChaInfoCommand;
import com.ljn.core.dto.common.exceptions.ParamsException;
import com.ljn.core.dto.common.exceptions.UserIsExistedException;
import com.ljn.core.dto.common.utils.assmbler.EnterpriseAssmbler;
import com.ljn.core.dto.common.utils.assmbler.StudentAssmbler;
import com.ljn.core.dto.common.utils.assmbler.TeacherAssmbler;
import com.ljn.core.dto.common.utils.assmbler.UserAssmbler;
import com.ljn.core.dto.common.utils.ency.MD5EncryptUtil;
import com.ljn.facade.UserFacade;
import com.ljn.service.EnterpriseService;
import com.ljn.service.StudentService;
import com.ljn.service.TeacherService;
import com.ljn.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by ljn on 2017/11/4.
 */
@Named
public class UserFacedeImpl implements UserFacade {
    transient static final Logger LOGGER = LoggerFactory.getLogger(UserFacedeImpl.class);
    @Inject
    StudentService studentService;
    @Inject
    TeacherService teacherService;
    @Inject
    EnterpriseService enterpriseService;
    @Inject
    UserService userService;

    @Override
    public Response register(Registercommand registercommand) {
        Response response = new Response();
        Byte type = registercommand.getType();
        switch (type) {
            case 0:
                try {
                    Long flag = studentService.register(StudentAssmbler.toStudent(registercommand));
                    if (flag > 0) {
                        response.setCode(Response.CodeEnum.SUCCESS.code);
                        response.setMsg("注册成功");
                        response.setData(flag);//返回注册成功之后的id
                    }
                } catch (UserIsExistedException e) {
                    LOGGER.error(e.getMessage(), e);
                    response.setCode(Response.CodeEnum.AUTHOR.code);
                    response.setMsg(e.getMessage());
                    return response;
                } catch (Exception e) {
                    LOGGER.error(e.getMessage(), e);
                    response.setCode(Response.CodeEnum.AUTHOR.code);
                    response.setMsg("添加用户失败。");
                    return response;
                }
                break;
            case 1:
                try {
                    Long flag = teacherService.register(TeacherAssmbler.toTeacher(registercommand));
                    if (flag > 0) {
                        response.setCode(Response.CodeEnum.SUCCESS.code);
                        response.setMsg("注册成功");
                        response.setData(flag);//返回注册成功之后的id
                    }
                } catch (UserIsExistedException e) {
                    LOGGER.error(e.getMessage(), e);
                    response.setCode(Response.CodeEnum.AUTHOR.code);
                    response.setMsg(e.getMessage());
                    return response;
                } catch (Exception e) {
                    LOGGER.error(e.getMessage(), e);
                    response.setCode(Response.CodeEnum.AUTHOR.code);
                    response.setMsg("添加用户失败。");
                    return response;
                }
                break;
            case 2:
                try {
                    Long flag = enterpriseService.register(EnterpriseAssmbler.toEnterprise(registercommand));
                    if (flag > 0) {
                        response.setCode(Response.CodeEnum.SUCCESS.code);
                        response.setMsg("注册成功");
                        response.setData(flag);//返回注册成功之后的id
                    }
                } catch (UserIsExistedException e) {
                    LOGGER.error(e.getMessage(), e);
                    response.setCode(Response.CodeEnum.AUTHOR.code);
                    response.setMsg(e.getMessage());
                    return response;
                } catch (Exception e) {
                    LOGGER.error(e.getMessage(), e);
                    response.setCode(Response.CodeEnum.AUTHOR.code);
                    response.setMsg("添加用户失败。");
                    return response;
                }
                break;
        }
        return response;
    }
    public Response<User> login(LoginCommand logincommand) {
        Response<User> response = new Response();
        try {
            User record = userService.login(UserAssmbler.toUserLogin(logincommand));
            if (record != null) {
                Byte type = record.getType();
                String id = record.getUserId();
                switch (type) {
                    case 0:
                        Student student = studentService.selectStudent(id);
                        record = StudentAssmbler.getStudent(record,student);
                        response.setCode(Response.CodeEnum.SUCCESS.code);
                        response.setMsg("登录成功");
                        response.setData(record);
                        break;
                    case 1:
                        Teacher teacher = teacherService.selectTeacher(id);
                        teacher = TeacherAssmbler.getTeacher(record,teacher);
                        response.setCode(Response.CodeEnum.SUCCESS.code);
                        response.setMsg("登录成功");
                        response.setData(teacher);
                        break;
                    case 2:
                        Enterprise enterprise = enterpriseService.selectEnterprise(id);
                        enterprise = EnterpriseAssmbler.getEnterprise(record,enterprise);
                        response.setCode(Response.CodeEnum.SUCCESS.code);
                        response.setMsg("登录成功");
                        response.setData(enterprise);
                        break;
                }
            }
            else {
                response.setCode(Response.CodeEnum.FAILED.code);
                response.setMsg("登录失败");
                response.setData(null);
            }
            return response;
        } catch (ParamsException e) {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.PARAM.code);
            response.setMsg(e.getMessage());
            return response;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.UNKNOWN.code);
            response.setMsg(e.getMessage());
            return response;
        }
    }

    @Override
    public Response queryUserDetail(User user) {
        Response response = new Response();
        return response;
    }

    @Override
    public Response checkUserPwd(IdCommand idCommand) {
        Response response = new Response();
        try {
            User user = new User();
            user.setUserId(idCommand.getUserId());
            user.setPassword(MD5EncryptUtil.encryptPassword(idCommand.getPassword(), idCommand.getUserId()));
            User result = userService.findUserByCond(user);
            if (result != null) {
                response.setCode(Response.CodeEnum.SUCCESS.code);
            } else {
                response.setCode(Response.CodeEnum.FAILED.code);
                response.setMsg("对不起！密码错误！");
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.UNKNOWN.code);
            response.setMsg(e.getMessage());
            return response;
        }
        return response;
    }

    @Override
    public Response changeUserPwd(IdCommand idCommand) {
        Response response =new Response();
        try{
            User user = new User();
            user.setUserId(idCommand.getUserId());
            user.setPassword(MD5EncryptUtil.encryptPassword(idCommand.getPassword(), idCommand.getPassword()));
            boolean result = userService.changeUserPwd(user);
            if (result) {
                response.setCode(Response.CodeEnum.SUCCESS.code);
                response.setMsg("修改成功！");
            } else {
                response.setCode(Response.CodeEnum.FAILED.code);
                response.setMsg("修改失败");
            }
        }
        catch (Exception e){
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.UNKNOWN.code);
            response.setMsg(e.getMessage());
            return response;
        }
        return  response;
    }
    @Override
    public Response changeUserInfo(ChaInfoCommand chaInfoCommand) {
        Response response = new Response();
        try{
            switch (chaInfoCommand.getType()){
            case 0:
                boolean resultStudent = studentService.changeUserInfo(StudentAssmbler.toStudent(chaInfoCommand));
                if (resultStudent) {
                    response.setCode(Response.CodeEnum.SUCCESS.code);
                    response.setMsg("修改成功！");
                } else {
                    response.setCode(Response.CodeEnum.FAILED.code);
                    response.setMsg("修改失败");
                }
                break;
            case 1:
                boolean resultTeacher = teacherService.changeUserInfo(TeacherAssmbler.toTeacher(chaInfoCommand));
                if (resultTeacher) {
                    response.setCode(Response.CodeEnum.SUCCESS.code);
                    response.setMsg("修改成功！");
                } else {
                    response.setCode(Response.CodeEnum.FAILED.code);
                    response.setMsg("修改失败");
                }
                break;
            case 2:
                boolean resultEnterprise = enterpriseService.changeUserInfo(EnterpriseAssmbler.toEnterprise(chaInfoCommand));
                if (resultEnterprise) {
                    response.setCode(Response.CodeEnum.SUCCESS.code);
                    response.setMsg("修改成功！");
                } else {
                    response.setCode(Response.CodeEnum.FAILED.code);
                    response.setMsg("修改失败");
                }
                break;
            }

        }
        catch (Exception e)
        {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.UNKNOWN.code);
            response.setMsg(e.getMessage());
            return response;
        }
        return  response;
    }
}